package com.jdew.thkj.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;

import butterknife.Bind;

/**
 * Created by yangzai on 2018/4/2 0002.
 */

public class CommonWebView extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.webview)
    WebView mWebView;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    private String url;
    private String head;

    @Override
    public int bindLayout() {
        return R.layout.activity_newcades;
    }

    @Override
    public void initView(View view) {
        initWeb(mWebView);
    }

    @SuppressLint("JavascriptInterface")
    public void initWeb(WebView webView) {
        head = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        title.setText(head);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.setInitialScale(5);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportMultipleWindows(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        //设置 缓存模式
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setBlockNetworkImage(false);
        webSettings.setDefaultTextEncodingName("utf-8");
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.addJavascriptInterface(this,"android");
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        loadUrl(url);

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String s) {
                //必须重写的方法 解决了优酷 百度视频不播放视频加载失败的问题
                if(url.startsWith("intent")||url.startsWith("youku")){

                    return true;
                }else{
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }
        });


    }

    public void loadUrl(String url) {
        mWebView.loadUrl(url);
    }

    @Override
    public void onDestroy() {
        mWebView.stopLoading();
        mWebView.removeAllViews();
        mWebView.destroy();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            default:
                break;
        }
    }

}
